package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.SetmealDao;
import com.atguigu.pojo.Setmeal;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.service.SetmealService;
import com.atguigu.utils.CascadeDownLoadUtils;
import com.atguigu.utils.DownLoadUtils;
import com.atguigu.utils.QiniuUtils;
import com.atguigu.utils.mergeDownLoadUtils;
import com.atguigu.vo.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zhengkaiyuan
 * @version 1.0
 * @create 2021-06-21 19:29
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public void edit(Setmeal setmeal, Integer[] travelGroupIds) {

        //删除该文件对应的图片（之前的）
        //获取最初的照片名
        String FileName = setmealDao.getFileName(setmeal.getId());
        QiniuUtils.deleteFileFromQiniu(FileName);
        if (travelGroupIds != null && travelGroupIds.length > 0) {
            //更新中间表当中的数据
            //1.删除之前中间表该套餐对应的数据
            setmealDao.deleteOriginal(setmeal.getId());
            //将当前新的数据插入到中间表当中
            updateSetemalAndTravelGroupBySetemalId(setmeal.getId(), travelGroupIds);
        }
        //处理套餐更新
        setmealDao.edit(setmeal);


    }

    @Override
    public String downLoadFile(String filePath, String queryString) throws Exception {
        if ("null".equals(queryString)) {
            queryString = "";
        }
        //查询出当前数据
        List<Setmeal> result = setmealDao.findByConditionByDownLoad(queryString);
        System.out.println("result = " + result);
        String excelName = "Setmeal";
        String excelToName = mergeDownLoadUtils.createExcelToName(excelName, result, filePath);
        return excelToName;
    }


    @Override
    public Setmeal mobileFindById(int id) {
        return setmealDao.mobileFindById(id);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal getSetmealInfoById(Integer id) {
        return setmealDao.getSetmealInfoById(id);
    }


    //用于处理更新的中间表数据
    private void updateSetemalAndTravelGroupBySetemalId(Integer setmealId, Integer[] travelGroupIds) {
        for (Integer travelGroupId : travelGroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmealId", setmealId);
            map.put("travelGroupId", travelGroupId);
            setmealDao.editSetemalAndTravelGroupBySetemalId(map);
        }

    }

    @Override
    public void add(Setmeal setmeal, Integer[] travelGroups) {
        //1.添加套餐数据
        setmealDao.add(setmeal);
        //2.绑定中间表数据
        if (travelGroups != null && travelGroups.length > 0) {
            setmealDao.setSetmealAndTravelGroup(setmeal.getId(), travelGroups);
        }
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(currentPage, pageSize);
        //查询数据
        Page<Setmeal> setmealPage = setmealDao.findPage(queryString);
        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    @Override
    public void delete(Integer id) {

        //先删除中间表信息
        setmealDao.deleteTravelGroupAndSetmeal(id);
        //删除套餐信息
        setmealDao.delete(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        return setmeal;
    }

    @Override
    public List<Integer> findTravelgroupBySetmealId(Integer id) {
        return setmealDao.findTravelgroupBySetmealId(id);
    }

}
